package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Efficacy;
import com.adnaan.backend.entity.Medicinal;
import com.adnaan.backend.entity.dto.OptionDto;
import com.adnaan.backend.entity.dto.PictureListDto;
import com.adnaan.backend.entity.vo.EfficacyVo;
import com.adnaan.backend.entity.vo.PicturesVo;
import com.adnaan.backend.entity.vo.TabooVo;
import com.adnaan.backend.service.EfficacyService;
import com.adnaan.backend.service.HerbalTabooService;
import com.adnaan.backend.service.MedicinalService;
import com.adnaan.backend.utils.ConverterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * author: adnaan
 * create: 2025-02-03 13:40
 * Description:
 */
@Tag(name = "药材管理")
@RestControllerMapping("/medicinal")
@ApiSupport(author = "adnaan", order = 5)
public class MedicinalController {

    @Resource
    private MedicinalService medicinalService;

    @Resource
    private EfficacyService efficacyService;

    @Resource
    private HerbalTabooService herbalTab;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "药材分页查询")
    public Result<Page<Medicinal>> pageQuery(@RequestBody PageParams<Medicinal> pageParams) {
        // 1.查询参数
        Medicinal params = pageParams.getParams();
        LambdaQueryWrapper<Medicinal> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, Medicinal::getName, params.getName());
        // 3.分页查询
        Page<Medicinal> page = medicinalService.page(pageParams.getPage(), lqw);
        page.getRecords().forEach(item -> {
            item.setEfficacy(medicinalService.getEfficacy(item.getId()));
            item.setDefaultPicture(medicinalService.getDefaultPicture(item.getId()));
            item.setTaboo(herbalTab.getById(item.getTabooId()));
        });
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增药材数据")
    public Result<Boolean> insert(@RequestBody Medicinal medicinal) {
        if (medicinalService.save(medicinal)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新药材数据")
    public Result<Boolean> update(@RequestBody Medicinal medicinal) {
        if (medicinalService.updateById(medicinal)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除药材数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (medicinalService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }

    /**
     * 查询所有功效
     *
     * @return efficacy
     */
    @GetMapping("/efficacy")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "查询所有功效")
    public Result<List<OptionDto>> efficacy() {
        List<Efficacy> list = efficacyService.list();
        List<OptionDto> optionDtos = ConverterUtil.convertToOptionDtoList(
                list,
                Efficacy::getName,
                efficacy -> String.valueOf(efficacy.getId())
        );
        return Result.success(optionDtos);
    }

    /**
     * 删除药材的功效
     *
     * @param id 药材id efficacyId 功效id
     */
    @GetMapping("/deleteEfficacy/{id}/{efficacyId}")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "删除药材的功效")
    public Result<Boolean> deleteEfficacy(@PathVariable Long id, @PathVariable Long efficacyId) {
        if (medicinalService.deleteEfficacy(id, efficacyId)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 修改药材的功效
     *
     * @param efficacyVo materialId 药材id efficacyIds 功效ids
     */
    @PostMapping("/updateEfficacy")
    @ApiOperationSupport(order = 7)
    @Operation(summary = "修改药材的功效")
    public Result<Boolean> updateEfficacy(@RequestBody EfficacyVo efficacyVo) {
        if (medicinalService.updateEfficacy(efficacyVo)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 添加图集
     */
    @PostMapping("/addPictures")
    @ApiOperationSupport(order = 8)
    @Operation(summary = "添加图集")
    public Result<Boolean> addPictures(@RequestBody PicturesVo picturesVo) {
        if (medicinalService.addPictures(picturesVo)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 图集列表
     */
    @GetMapping("/pictures/{materialId}")
    @ApiOperationSupport(order = 9)
    @Operation(summary = "图集列表")
    public Result<List<PictureListDto>> pictures(@PathVariable Long materialId) {
        List<PictureListDto> pictures = medicinalService.pictures(materialId);
        return Result.success(pictures);
    }

    /**
     * 删除图集中的图片
     *
     * @param picId 图片id
     */
    @GetMapping("/deletePicture/{picId}")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "删除图集中的图片")
    public Result<Boolean> deletePicture(@PathVariable Long picId) {
        if (medicinalService.deletePicture(picId)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 查看所有药材
     */
    @GetMapping("/all")
    @ApiOperationSupport(order = 11)
    @Operation(summary = "查看所有药材")
    public Result<List<OptionDto>> all() {
        List<Medicinal> list = medicinalService.list();
        List<OptionDto> optionDtos = ConverterUtil.convertToOptionDtoList(
                list,
                Medicinal::getName,
                medicinal -> String.valueOf(medicinal.getId())
        );
        return Result.success(optionDtos);
    }

    /**
     * 添加/修改药材禁忌
     */
    @PostMapping("/addTaboo")
    @ApiOperationSupport(order = 12)
    @Operation(summary = "添加/修改药材禁忌")
    public Result<Boolean> addTaboo(@RequestBody TabooVo tabooVo) {
        // 1.添加/修改禁忌后返回id
        Long tabooId = herbalTab.addOrSaveTaboo(tabooVo.getId(), tabooVo.getPeople(), tabooVo.getMatters());
        // 2.修改药材的禁忌id
        medicinalService.lambdaUpdate().eq(Medicinal::getId, tabooVo.getMedicinalId()).set(Medicinal::getTabooId, tabooId).update();
        return Result.success();
    }

}
